package com.javabase.y2022.m02.day0217.t1;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2022/2/17 下午 08:47
 * @version: 1.0
 */
public class Account {
    private String accountNo;
    private double balance;
    // 标识账户中是否已有存款的标志
    private boolean flag = false;

    public Account(String accountNo, double balance) {
        this.accountNo = accountNo;
        this.balance = balance;
    }

    public String getAccountNo() {
        return accountNo;
    }

    public void setAccountNo(String accountNo) {
        this.accountNo = accountNo;
    }
    public double getBalace(){
        return this.balance;
    }
    public synchronized void draw(double drawAmount){
        try{
           if(!flag){
               wait();
           } else {
               System.out.println(Thread.currentThread().getName()
               + " 取钱:" + drawAmount);
               balance -= drawAmount;
               System.out.println("账户余额为:" + balance);
               flag = false;
               notifyAll();
           }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public synchronized void deposit(double depositAmount){
        try{
            if(flag){
                wait();
            }
            else
            {

            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
